package com.wjp.banquet.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjp.banquet.common.enums.BanquetIsSealEnum;
import com.wjp.banquet.common.enums.SortTypeEnum;
import com.wjp.banquet.common.enums.YesOrNoEnum;
import com.wjp.banquet.common.util.ParamCheckUtil;
import com.wjp.banquet.common.util.StringUtil;
import com.wjp.banquet.dto.GiftMoneyAddDTO;
import com.wjp.banquet.dto.GiftMoneyPageQueryDTO;
import com.wjp.banquet.entity.Banquet;
import com.wjp.banquet.entity.GiftMoney;
import com.wjp.banquet.mapper.BanquetMapper;
import com.wjp.banquet.mapper.GiftMoneyMapper;
import com.wjp.banquet.service.GiftMoneyService;
import com.wjp.banquet.vo.BanquetVO;
import com.wjp.banquet.vo.GiftMoneyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 王君平
 * @Date 2024/4/24 8:29
 * @Description
 */
@Service
public class GiftMoneyServiceImpl implements GiftMoneyService {

    @Autowired
    private GiftMoneyMapper giftMoneyMapper;

    @Autowired
    private BanquetMapper banquetMapper;

    @Override
    public PageInfo<GiftMoneyVO> pageList(GiftMoneyPageQueryDTO dto) {
        if(StringUtil.isEmpty(dto.getSortName())){
            dto.setSortName("id");
            dto.setSortType(SortTypeEnum.DESC.getCode());
        }
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        PageHelper.orderBy(dto.getOrderBy());

        List<GiftMoney> list = giftMoneyMapper.getPageList(dto);
        PageInfo<GiftMoney> pageInfo = new PageInfo<>(list);
        PageInfo<GiftMoneyVO> voPageInfo = new PageInfo<>();
        if(CollectionUtils.isEmpty(list)){
            return voPageInfo;
        }
        BeanUtils.copyProperties(pageInfo,voPageInfo);
        voPageInfo.setList(this.poToVo(list));
        return voPageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(GiftMoneyAddDTO dto) {
        Integer banquetId = dto.getBanquetId();
        Banquet banquet = banquetMapper.selectById(banquetId);
        ParamCheckUtil.objectNull(banquet,"酒席不存在");
        ParamCheckUtil.isTrue(BanquetIsSealEnum.YES.isCode() == banquet.getIsSeal(),"此酒席已经封存，不允许修改了");

        Integer giftMoney = dto.getGiftMoney();
        ParamCheckUtil.numberLessOrEqualZero(giftMoney,"礼金不能小于等于0");

        GiftMoney po = new GiftMoney();
        BeanUtils.copyProperties(dto,po);
        po.fillUpToSave();
        giftMoneyMapper.insert(po);
    }

    @Override
    public List<GiftMoneyVO> allList(GiftMoneyPageQueryDTO dto) {
        if(StringUtil.isEmpty(dto.getSortName())){
            dto.setSortName("id");
            dto.setSortType(SortTypeEnum.DESC.getCode());
        }
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        PageHelper.orderBy(dto.getOrderBy());

        List<GiftMoney> list = giftMoneyMapper.getAllList(dto);
        if(CollectionUtils.isEmpty(list)){
            return Collections.EMPTY_LIST;
        }
        return this.poToVo(list);
    }

    private List<GiftMoneyVO> poToVo(List<GiftMoney> list) {
        return list.stream().map(v -> poToVo(v)).collect(Collectors.toList());
    }

    private GiftMoneyVO poToVo(GiftMoney po) {
        GiftMoneyVO vo = new GiftMoneyVO();
        BeanUtils.copyProperties(po,vo);
        vo.setIsProxyName(YesOrNoEnum.YES.isCode() == po.getIsProxy() ? "代" : "");
        return vo;
    }
}
